﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using ERP.ERPSystem.ProductionSystem.Dto;
using ERP.Team.ProductionSystem;

namespace ERP.ERPSystem.ProductionSystem
{
    /// <summary>
    /// 班组服务实现层
    /// </summary>
    public class TeamService : ApplicationService, ITeamService
    {
        private readonly IRepository<TeamModel, int> _teamRepository;

        public TeamService(IRepository<TeamModel, int> teamRepository)
        {
            _teamRepository = teamRepository;
        }

        /// <summary>
        /// 获取班组分页列表
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="limit">每页数量</param>
        /// <param name="teamCode">班组编号（可选）</param>
        /// <param name="teamName">班组名称（可选）</param>
        /// <returns></returns>
        public async Task<TeamPageResultDTO> GetTeamPageListAsync(int page = 1, int limit = 10, string teamCode = null, string teamName = null)
        {
            try
            {
                var query = await _teamRepository.GetQueryableAsync();

                // 根据条件过滤
                if (!string.IsNullOrEmpty(teamCode))
                {
                    query = query.Where(t => t.TeamCode.Contains(teamCode));
                }
                if (!string.IsNullOrEmpty(teamName))
                {
                    query = query.Where(t => t.TeamName.Contains(teamName));
                }

                var totalCount = query.Count();
                var teams = query
                    .OrderBy(t => t.Id)
                    .Skip((page - 1) * limit)
                    .Take(limit)
                    .ToList();

                var teamDtos = teams.Select(t => new TeamDTO
                {
                    Id = t.Id,
                    TeamCode = t.TeamCode,
                    TeamName = t.TeamName,
                    TeamDesc = t.TeamDesc
                }).ToList();

                return new TeamPageResultDTO
                {
                    Code = 0,
                    Msg = "查询成功",
                    Count = totalCount,
                    Data = teamDtos
                };
            }
            catch (Exception ex)
            {
                return new TeamPageResultDTO
                {
                    Code = 1,
                    Msg = $"查询班组列表失败：{ex.Message}",
                    Count = 0,
                    Data = new List<TeamDTO>()
                };
            }
        }

        /// <summary>
        /// 根据ID获取班组详情
        /// </summary>
        /// <param name="id">班组ID</param>
        /// <returns></returns>
        public async Task<TeamDTO> GetTeamByIdAsync(int id)
        {
            try
            {
                var team = await _teamRepository.GetAsync(id);
                if (team == null)
                {
                    return null;
                }

                return new TeamDTO
                {
                    Id = team.Id,
                    TeamCode = team.TeamCode,
                    TeamName = team.TeamName,
                    TeamDesc = team.TeamDesc
                };
            }
            catch
            {
                return null;
            }
        }
    }
}
